package com.jmyd.txlcn.base.task;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池
 *
 * @author samphin
 * @date 2022/6/14 11:41
 */
public class CustomThreadPool extends ThreadPoolExecutor {

    private static final String DEFAULT_POOL_NAME = "default_thread";

    /**
     * @param corePoolSize    核心线程池
     * @param maximumPoolSize 线程池最大数量
     * @param keepAliveTime   线程存活时间
     * @param unit            TimeUnit
     * @param workQueue       工作队列,自定义大小
     * @param poolName        线程工厂自定义线程名称
     */
    public CustomThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, String poolName) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        setThreadFactory(new CustomThreadFactory(poolName, false));
    }

    public CustomThreadPool(int corePoolSize, BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, corePoolSize, 0L, TimeUnit.SECONDS, workQueue,DEFAULT_POOL_NAME);
        setThreadFactory(new CustomThreadFactory(DEFAULT_POOL_NAME, false));
    }
}
